package eqpkanban

import grails.converters.JSON

/**
 * @Program: eqpkanban
 * @Description: 区域
 * @Author: 樊双峰
 * @Date: 2018-08-10 23:49
 */
class AreaController {

    def dynamicQueryService

    def getAreaJson() {
        params.SYS_QUERY_NAME = 'areaList'
        def getArea = dynamicQueryService.queryForList(params)
        render getArea as JSON
    }

    /**
     * 区域信息
     * @return
     */
    def areaInfo(){
        // 设置编码集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // 设置分页
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startNo = (pageSize * (pageCurrent - 1)) + 1
        params.endNo = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        // 区域信息列表
        params.SYS_QUERY_NAME = 'getAreaInfo'
        def getAreaInfo = dynamicQueryService.queryForList(params)
        // 总条数
        params.SYS_QUERY_NAME = 'areaCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        [pageSize: pageSize, totalCount: totalCount, pageSizeList: pageSizeList,
         getAreaInfo:getAreaInfo]
    }

    /**
     * 新增区域
     * @param area
     * @return
     */
    def addArea(Area area) {
        render(view: 'addArea', model: [Area: area])
    }

    /**
     * 编辑区域
     * @param area
     * @return
     */
    def editArea(Area area) {
        render(view: 'editArea', model: [Area: area])
    }

    /**
     * 保存区域
     * @param area
     * @return
     */
    def saveArea(Area area) {
        area.save()
        render """{
                    "statusCode":"200",
                    "message":"${
            message(code: 'default.qcreated.message', args: [message(code: 'Area.label', default: 'Area'), area.id])
        }",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }

    /**
     * 删除区域
     * @param area
     * @return
     */
    def deleteArea(Area area) {
        area.delete()
        render """{
                    "statusCode":"200",
                    "message":"${
            message(code: 'default.created.message', args: [message(code: 'Area.label', default: 'Area'), area.id])
        }",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }
}